package com.a.module_storage;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProductTypeAdapter extends RecyclerView.Adapter<ProductTypeAdapter.ViewHolder> {

    private final List<ProductType> productTypeLists;
    private final Handler handler;
    private final Context context;

    private static final String TAG = ProductTypeAdapter.class.getSimpleName();

    public ProductTypeAdapter(List<ProductType> productTypeLists, Context context, Handler handler) {
        this.productTypeLists = productTypeLists;
        this.context = context;
        this.handler = handler;
    }

    //绑定item的视图零件
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_productType;
        View view_bottomColor;
        LinearLayout ll_productType_parent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_productType = itemView.findViewById(R.id.tv_type);
            view_bottomColor = itemView.findViewById(R.id.view_bottomColor);
            ll_productType_parent = itemView.findViewById(R.id.ll_productType_parent);
        }


    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.storage_item_product_type, parent, false);
        return new ViewHolder(view);
    }


    //    public Map<Integer, Boolean> map = new HashMap<>();
    private int select=0, lastSelect=0;

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String type = productTypeLists.get(position).getClassTypeName();
        String classTypeNo = productTypeLists.get(position).getClassTypeNo();
        holder.tv_productType.setText(type);
        holder.tv_productType.setOnClickListener(v -> {
            select = position;
            if (lastSelect != select) {
                notifyItemChanged(lastSelect);
                notifyItemChanged(select);
                doGetProductItem(classTypeNo);
                lastSelect = select;
            }
        });
        if (position != select) {//不包含
            holder.tv_productType.setTextColor(context.getResources().getColor(R.color.gray));
            holder.view_bottomColor.setBackgroundColor(context.getResources().getColor(R.color.grey_lighten_3));
        } else {//选中的
            holder.tv_productType.setTextColor(context.getResources().getColor(R.color.themeColor));
            holder.view_bottomColor.setBackgroundColor(context.getResources().getColor(R.color.themeColor));
        }
    }

    @Override
    public int getItemCount() {
        return productTypeLists.size();
    }
    public void reset(){
        select=0;
        lastSelect=0;
    }

    private void doGetProductItem(String classTypeNo) {
        Message message = Message.obtain(handler, 1);  //向StorageFragment发送1，让StorageFragment来处理发送获取ProductItem；
        Bundle bundle = new Bundle();
        bundle.putString("classTypeNo", classTypeNo);
        Log.d(TAG, "classTypeNo=[==========" + classTypeNo);
        message.setData(bundle);
        handler.sendMessage(message);
    }
}
